package Actions;

import java.awt.FileDialog;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JOptionPane;

import Room.Room;

public class Files {

	private TypeWriter.TypeWriter parent;
	private Room editor;
	

	public Files( TypeWriter.TypeWriter parent, Room editor) {
		this.parent = parent;
		this.editor = editor;
	}
	

	public void saveFileAs()
	{
		FileDialog fileDialog = new FileDialog(parent, "Save", FileDialog.SAVE);
		fileDialog.setVisible(true);
		if( fileDialog.getFile() != null )
		try{
			int lastCaretPosition = editor.getCaretPosition();
			String filePath = fileDialog.getDirectory() + fileDialog.getFile();
			saveFile(filePath);
			editor.setCaretPosition(lastCaretPosition);
		}catch(NullPointerException e)
		{
			JOptionPane.showMessageDialog(parent, "bad file");
		}
	}
	
	// return false on user cancel
	public boolean saveOld()
	{
		if(editor.getModified())
		{
			String f = editor.getFilePath();
			if( f == null ) f = "untitled";
			else{
				String[] flist = f.split("/|\\\\",0);
				if( flist.length > 0 )
					f = flist[flist.length-1];	
			}
			
			int option = JOptionPane.showConfirmDialog(parent, 
					"Would you like to save " + f + "?",
					"Save",
					JOptionPane.YES_NO_CANCEL_OPTION);
			if( option == JOptionPane.YES_OPTION )
				saveFile(editor.getFilePath());
			else if( option == JOptionPane.CANCEL_OPTION )
				return false;
		}
		return true;
	}
	
	
	public void openFile()
	{
		if( !saveOld() ) return;

		FileDialog fileDialog = new FileDialog(parent, "Open", FileDialog.LOAD);
		fileDialog.setVisible(true);
		if(fileDialog.getFile() == null ) return;
		try{
			String filePath = fileDialog.getDirectory() + fileDialog.getFile();
			readFile(filePath);
		}catch(NullPointerException e)
		{
			JOptionPane.showMessageDialog(parent, "bad file");
		}
	}
	
	public void readFile(String filePath )
	{
		editor.caretSafe = true;
		try {
			
			FileReader r = new FileReader(filePath);
			editor.read(r, null);
			r.close();
			editor.setFilePath(filePath);
			editor.setModified(false);
			
			int lastCaretPosition = editor.getDocument().getLength();
			editor.setSelectionStart(lastCaretPosition);
			editor.setSelectionEnd(0);
			
			parent.changeTitle();
		}
		catch(IOException e)
		{
			Toolkit.getDefaultToolkit().beep();
			JOptionPane.showMessageDialog(parent, "Cannot find " + filePath);
		}
		catch(NullPointerException e)
		{
			Toolkit.getDefaultToolkit().beep();
			JOptionPane.showMessageDialog(parent, "Bad file: " + filePath);
		}
		editor.caretSafe = false;
	}
	
	// creates new file
	public void newFile()
	{
		if( !saveOld() ) return;
		editor.clear();
		editor.setFilePath(null);
		editor.setModified(false);
		parent.changeTitle();
	}
	
	public void saveFile(String filePath)
	{
		int lastCaretPosition = editor.getCaretPosition();
		if(filePath == null){
			saveFileAs();
			return;
		}
		try {
			FileWriter w = new FileWriter(filePath);
			editor.write(w);
			w.close();
			editor.setFilePath(filePath);
			editor.setCaretPosition( lastCaretPosition );
			editor.setModified(false);
			parent.changeTitle();
		}
		catch(IOException e)
		{
			JOptionPane.showMessageDialog(parent, "Error saving file");
		}
	}
	
	
	// checks if a file exists
	public boolean exists(String f)
	{
		File file = new File(f);
		return file.exists();
	}
}
